package meterMonitor.biz;

import meterMonitor.mapper.MeterMapper;
import meterMonitor.mapper.MonitorHourMapper;
import meterMonitor.model.Meter;
import meterMonitor.model.MonitorHour;
import meterMonitor.util.DateUtils;
import meterMonitor.vo.MonitorMeter;
import meterMonitor.vo.QueryForChartRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

/**
 * Created by Administrator on 2019/1/15 0015.
 */
@Service
public class MonitorHourBiz {
    public static final Logger logger = LoggerFactory.getLogger(MonitorHourBiz.class);
    @Autowired
    private MonitorHourMapper hourMapper;

    @Autowired
    private MeterMapper meterMapper;

    public List<MonitorHour> queryForHourChart(QueryForChartRequest requestMod){
        if(!requestMod.getEndDate().equals(requestMod.getStartDate())){
            return null;
        }
        if(requestMod.getType()==40){
            return null;
        }
        if(requestMod.getAnotherName()!=null){
            boolean isAll=false;
            for(String no :requestMod.getAnotherName()){
                if(no.equals("-1")){
                    isAll=true;
                    break;
                }
            }
            if(isAll){
                requestMod.setAnotherName(null);
            }
        }

        List<MonitorHour> list=hourMapper.queryForCharts(requestMod);
        //double保留2位小数四舍五入
        DecimalFormat df = new DecimalFormat("#0.00");
        for (MonitorHour monitorHour:list){
            monitorHour.setIntervalQuantity(Double.valueOf(df.format(monitorHour.getIntervalQuantity())));
        }
        return list;
    }

    //图表
    public Map queryForHour(QueryForChartRequest requestMod){
        if(!requestMod.getEndDate().equals(requestMod.getStartDate())){
            return null;
        }
        if(requestMod.getType()==40){
            return null;
        }
        if(requestMod.getAnotherName()!=null){
            boolean isAll=false;
            for(String no :requestMod.getAnotherName()){
                if(no.equals("-1")){
                    isAll=true;
                    break;
                }
            }
            if(isAll){
                requestMod.setAnotherName(null);
            }
        }

        List<MonitorMeter> list=hourMapper.queryForOneDay(requestMod);
        //double保留2位小数四舍五入
        DecimalFormat df = new DecimalFormat("#0.00");
        Double Quantity = 0d;
        for (MonitorMeter monitorMeter:list){
            monitorMeter.setIntervalQuantity(Double.valueOf(df.format(monitorMeter.getIntervalQuantity())));
            Quantity = monitorMeter.getIntervalQuantity();
        }
        Map map= formatChart(requestMod,list, Quantity);

        return map;
    }

    /**
     *
     * @param requestMod
     * @param data
     * @param Quantity 当日实际用水量
     * @return
     */
    private Map formatChart(QueryForChartRequest requestMod, List<MonitorMeter> data, Double Quantity){
        Map<String,Object> map=new HashMap<>();
        List<MonitorMeter> resultList=new ArrayList<MonitorMeter>();
        List<String> meterNames=this.getMeterNames(requestMod);
        Date sDate= DateUtils.parse(requestMod.getStartDate(),DateUtils.PATTERN_CLASSICAL_SIMPLE);
        Date vDate;
        MonitorMeter mod;
        List<String> dateList=new ArrayList<>();
        List<Map> dataList=new ArrayList<>();
        List<Double> qty;
        Map<String,Object> mapData;
        boolean isSetDate=false;
        int iv=8;
        int cnt=12;
        if(requestMod.getType()==20){
            iv=20;
        }
        if(requestMod.getType()==30){
            cnt=24;
        }
        int ivt;
        String hourStr;
        int i = 0;
        for(String name:meterNames){
            i++;
            vDate=sDate;
            ivt=iv;
            qty=new ArrayList<>();
            mapData=new HashMap<>();

                if(meterNames.size() == i&&requestMod.getEndDate().equals(requestMod.getStartDate())){
                    hourStr=requestMod.getEndDate();
                    dateList.add(hourStr);
                    mod=this.generateMM(data,name,hourStr);
                    qty.add(Quantity); //
                    resultList.add(mod);
                }
            mapData.put(name,qty);
            dataList.add(mapData);
            isSetDate=true;
        }
        map.put("dates",dateList);
        map.put("datas",dataList);
        return map;
        // return resultList;
    }

    private MonitorMeter generateMM(List<MonitorMeter> data,String meterName,String hourStr){
        MonitorMeter mod;
        for(MonitorMeter model:data){
            if(model.getAnotherName().equals(meterName)){
                return model;
            }
        }
        mod=new MonitorMeter();
        mod.setAnotherName(meterName);
        mod.setOwnerTimer(hourStr);
        mod.setIntervalQuantity(0D);
        mod.setIntervalSec(0D);
        mod.setStandard(0);
        mod.setMonthMeterStandar(0);
        return  mod;
    }


    public List<String> getMeterNames(QueryForChartRequest requestMod){
        if (requestMod.getAnotherName() == null) {
            List<Meter> list=this.meterMapper.queryAllMeter();
            List<String> result=new ArrayList<>();
            for(Meter meter:list){
                result.add(meter.getAnotherName());
            }
            return result;
        }
        return requestMod.getAnotherName();
    }
}
